import { Component, OnInit, OnDestroy } from '@angular/core';

import { Util } from '../../utils/util';

import { BrandComponentService } from './brand.component.service';


import { BrandBase } from './brand.base';

@Component({
  selector: 'app-brand',
  templateUrl: './brand.component.html',
  providers: [BrandComponentService]
})
export class BrandComponent extends BrandBase {

  option: any = {
    defaultText: true
  };

  param: any = {
    brandNameSearch: '',
    page: 0,
    start: 0,
    rows: 10,
    sort: 'sellMoney',
    sortType: 'desc'
  };

  constructor(public util: Util, public serivce: BrandComponentService) {

    super(util, serivce);

    if (this.util.sessionStore().has('brand')) {

      let _store = this.util.sessionStore().get('brand');

      Object.assign(this.param, _store.param);

      console.log(_store);

      if (_store.cat && (typeof _store.cat.wholeName != "undefined")) {
        this.option.cat = _store.cat.wholeName == '' ? "/" : _store.cat.wholeName;
      }



      this.getHttp(this.param);
    }

  }


  ngOnDestroy() {
    this.util.sessionStore().set('brand', { param: this.param, cat: this.cat });
    if (this.busy) {
      this.busy.unsubscribe();
    }
  }


  // 品牌名搜索
  brandSearchStore: string;
  search() {

    if (this.util.equals(this.param.brandNameSearch, this.brandSearchStore)) {
      return;
    }
    this.brandSearchStore = this.param.brandNameSearch;
    this.getHttp(this.param);

  }

  getHttp(param) {

    this.busy = this.serivce.getBrand(param).subscribe((res: any) => {

      for (let i in res.data.value) {

        let item = res.data.value[i];

        item.price = this.util.avgPrice(item.sellMoney, item.sellAmount);

        item.monthPrice = this.util.avgPrice(item.monthSellMoney, item.monthSellAmount);

      }

      this.vm.table.brands = res.data.value;

      this.vm.table.count = res.data.count;

    }, (err: any) => {

    });
  }

}
